package who.tmap.toolkit.dao;

import java.awt.geom.Point2D;

import org.jdom.Element;

import who.tmap.bo.BizObjects;
import who.tmap.bo.TextStyle;
import who.tmap.bo.impl.BizObjectFactory;
import who.tmap.dao.TMapDAOException;
import who.tmap.toolkit.shape.MapShape;
import who.tmap.toolkit.shape.MapShapes;
import who.tmap.toolkit.shape.annotation.LabelsListShape;

public class LabelsListDAO extends BaseMapShapeDAO {

	public MapShape load( Element elt ) throws TMapDAOException {
		LabelsListShape lls =null;
		
		try {
	    	lls =(LabelsListShape)msf.getGraphic( MapShapes.LABELS_LIST );
	    	
	    	lls.setName( elt.getAttributeValue( "name" ) );
	    	lls.setPt( new Point2D.Double( elt.getAttribute( "x" ).getDoubleValue(), 
	    								   -elt.getAttribute( "y" ).getDoubleValue() ) ) ;
	    	lls.setVisible( elt.getAttribute( "visible" ).getBooleanValue() );
	    	lls.setBackgroundTransparent( elt.getAttribute( "transparentBackground" ).getBooleanValue() );
	    	
	    	TextStyle ts =(TextStyle)BizObjectFactory.getInstance().get( BizObjects.TEXT_STYLE );
	    	lls.setStyle( ts );
	    	
	        Element fontElt =elt.getChild( "Font" );
	        ts.setFontName( fontElt.getAttribute( "name" ).getValue() );
	        ts.setFontSize( fontElt.getAttribute( "size" ).getIntValue() );
	        ts.setFontStyle( fontElt.getAttribute( "style" ).getIntValue() );
	        
	        lls.resetLabelShapes();
        
		} catch( Exception e ) {
			throw new TMapDAOException( e );
		}
        
        return lls;
	}
	
	public Element save( MapShape mapShape ) throws TMapDAOException {
		LabelsListShape lls =(LabelsListShape)mapShape;
		
		Element elt =new Element( "LabelsList" );
		
		elt.setAttribute( "name", lls.getName() );
		elt.setAttribute( "x", Double.valueOf( lls.getPt().getX() ).toString() );
		elt.setAttribute( "y", Double.valueOf( -lls.getPt().getY() ).toString() );
		
		elt.setAttribute( "visible", Boolean.valueOf( lls.isVisible() ).toString() );
		elt.setAttribute( "transparentBackground", Boolean.valueOf( lls.isBackgroundTransparent() ).toString() );
		
		Element fontElt =new Element( "Font" );
		elt.addContent( fontElt );
		
		fontElt.setAttribute( "name", lls.getStyle().getFontName() );
		fontElt.setAttribute( "size", Integer.valueOf( lls.getStyle().getFontSize() ).toString() );
		fontElt.setAttribute( "style", Integer.valueOf( lls.getStyle().getFontStyle() ).toString() );
		
		return elt;
	}
	
}
